package com.desmart.renjiao.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.android.arouter.launcher.ARouter;
import com.desmart.renjiao.R;
import com.desmart.renjiao.main.MainActivity;
import com.lemon.library.constants.PreferencesConstants;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonActivity;
import com.lemon.library.utils.PreferencesUtils;
import com.lemon.library.utils.ToastUtils;

public class LoginActivity extends CommonActivity<LoginM,LoginP> implements Login.LoginV {

    private Button btnLogin;
    private EditText etUserName;
    private EditText etPassword;

    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView(Activity activity) {
        swipeBackLayout.setEnableGesture(false);
        btnLogin = activity.findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);

        etUserName = activity.findViewById(R.id.et_username);
        etPassword = activity.findViewById(R.id.et_password);

        String loginUsername = PreferencesUtils.getString(PreferencesConstants.login_username);
        String loginPassword = PreferencesUtils.getString(PreferencesConstants.login_password);
        if (!TextUtils.isEmpty(loginUsername)){
            etUserName.setText(loginUsername);
            etPassword.setText(loginPassword);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setStatus();
        super.onCreate(savedInstanceState);
    }

    private void setStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == btnLogin) {

            String userName = etUserName.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password)){
                ToastUtils.showGravity("请输入用户名和密码");
                return;
            }
            mPresenter.login(userName,password);

        }

    }

    @Override
    public void onNetStart(String tag, String startMsg) {
        super.onNetStart(tag, startMsg);
        showLoading();
    }

    @Override
    public void onNetFinish(String tag, String startMsg) {
        super.onNetFinish(tag, startMsg);
        hideLoading();
    }

    @Override
    public void onLoginSuccess() {
        ARouter.getInstance().build(ARoutePath.Main.MainActivity).navigation();
        finish();
    }
}
